/**
 * @Author: ls <ls-mac>
 * @Date:   2020-04-27 14:30:27
 * @Last modified by:   ls-mac
 * @Last modified time: 2020-05-09 14:27:34
 */

module.exports = function(grunt) {
  // load
  require("load-grunt-tasks")(grunt);

  // config
  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    htmlRoot: grunt.option("target") || "html",
    distRoot: grunt.option("target") || "dist",
    assetsRoot: "assets", // 存放公共资源
    scssRoot: "scss", //

    clean: {
      before: {
        src: [
          "babel",
          "assets/js/common.min.js",
          "assets/js/layout.min.js",
          "assets/js/pages",
          "assets/css/common*.*",
          "assets/css/pages",
          "html"
        ]
      },
      after: {
        src: ["babel", ".sass-cache"]
      }
    },
    babel: {
      options: {
        sourceMap: false,
        presets: ["@babel/preset-env"],
        babelrc: true // 必须启用babelrc文件 (babel^7取消严格模式)
      },
      docs: {
        files: [
          {
            expand: true,
            cwd: "js/public/",
            src: ["*.js"],
            dest: "babel/", //输出到此目录下
            ext: ".js" // 输出的后缀名
          }
        ]
      },
      pages: {
        files: [
          {
            expand: true,
            cwd: "js/pages/",
            src: ["*.js"],
            dest: "babel/pages/", //输出到此目录下
            ext: ".js" // 输出的后缀名
          }
        ]
      }
    },
    uglify: {
      options: {
        sourceMap: false,
        mangle: true, //混淆变量名
        comments: "false" //false（删除全部注释），some（保留@preserve @license @cc_on等注释）
      },
      docs: {
        files: [
          {
            expand: true,
            cwd: "babel/",
            src: ["*.js", "!*.min.js"],
            dest: "<%= assetsRoot %>/js/",
            ext: ".min.js"
          }
        ]
      }
    },
    copy: {
      pages: {
        files: [
          {
            expand: true,
            cwd: "./babel/pages",
            src: ["*", "**/*", "**/**/*"],
            dest: "<%= assetsRoot %>/js/pages/"
          }
        ]
      }
    },
    sass: {
      options: {
        // Target options
        style: "expanded"
      },
      docs: {
        files: [
          {
            expand: true,
            cwd: "<%= scssRoot %>/public/",
            src: ["**/*.scss", "!**/_*.scss"],
            dest: "<%= assetsRoot %>/css",
            ext: ".css"
          }
        ]
      },
      pages: {
        files: [
          {
            expand: true,
            cwd: "<%= scssRoot %>/pages/",
            src: ["**/*.scss"],
            dest: "<%= assetsRoot %>/css/pages/",
            ext: ".css"
          }
        ]
      }
    },
    jade: {
      docs: {
        options: {
          pretty: true
        },
        files: [
          {
            expand: true,
            cwd: "templates/docs",
            src: ["*.jade", "!_*.jade"],
            dest: "<%= htmlRoot %>/docs",
            ext: ".html"
          }
        ]
      },
      pages: {
        options: {
          pretty: true
        },
        files: [
          {
            expand: true,
            cwd: "templates/pages",
            src: ["*.jade", "!_*.jade"],
            dest: "<%= htmlRoot %>/pages",
            ext: ".html"
          }
        ]
      }
    },
    connect: {
      server: {
        options: {
          port: 3000,
          hostname: "localhost",
          base: ".",
          routes: "/html",
          open: "http://localhost:3000/html/pages/index.html",
          browser: "google chrome"
        }
      }
    },
    watch: {
      options: {
        livereload: 3456
      },
      css: {
        files: "scss/**/*.scss",
        tasks: ["sass"]
      },
      js: {
        files: "js/**/*.js",
        tasks: ["babel:docs", "uglify:docs", "babel:pages", "copy:pages"]
      },
      html: {
        files: ["templates/*.jade", "templates/**/*.jade"],
        tasks: ["jade:docs", "jade:pages"]
      }
    }
  });

  grunt.registerTask("docs-js", ["babel:docs", "uglify:docs"]);
  grunt.registerTask("docs-css", ["sass:docs"]);
  grunt.registerTask("docs-html", ["jade:docs"]);

  grunt.registerTask("pages-js", ["babel:pages", "copy:pages"]);
  grunt.registerTask("pages-css", ["sass:pages"]);
  grunt.registerTask("pages-html", ["jade:pages"]);

  grunt.registerTask("docs", ["docs-css", "docs-html", "docs-js"]);

  grunt.registerTask("pages", ["pages-css", "pages-html", "pages-js"]);

  grunt.registerTask("dev", [
    "clean:before",
    "docs",
    "pages",
    "clean:after",
    "connect",
    "watch"
  ]);
};
